package mybatis.bean;

import java.io.IOException;
import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class Test {

	public static void main(String[] args) throws IOException {

		// 使用类加载器加载mybatis的配置文件（它也加载关联的映射文件）
		String resource = "mybatis-config.xml";
		Reader reader = Resources.getResourceAsReader(resource);
		SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
		SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(reader);
		SqlSession session = sqlSessionFactory.openSession();
		/**
		 * 映射sql的标识字符串org.guus.mapping.userMapper.getUser
		 * getUser是select标签的id属性值，通过select标签的id属性值就可以找到要执行的SQL
		 */
		String statement = "mybatis.mapping.userMapper.";// 映射sql的标识字符串
		// 执行查询返回一个唯一user对象的sql
//		User user = session.selectOne(statement+"selectUser", 2);
		
		User user = new User();
		user.setId(1);
		user.setName("名字");
		user.setAge(22);
		session.insert(statement+"insertUser",user);

		
//		session.update(statement+"updateUser", user);
		
		session.commit();
		System.out.println(user.toString());
	}

}
